﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace ToDoClear
{
    public  enum ListState
    {
       Idle,

    };
    public class ToDoList: Canvas
    {
        ListState state;
        //y方向的偏移
        private  double offset = 0;

        public double Offset
        {
            get { return offset; }
            set{offset=value;}
        }
        public double speed = 0;
        public ToDoList()
            : base()
        {
            this.Width = ConstVar.ScreenW;
            this.Height = ConstVar.ScreenH;
            state = ListState.Idle;
            offset = 0;

        }
        #region list 相关处理


        public void AddItem(ToDoItem item)
        {
            this.Children.Add(item);
         
        }

        public void RemoveItem(ToDoItem item)
        {
           this.Children.Remove(item);
        }
        public double AllHeight
        {
            get {
                return mAllHeight;
            }
           private  set {
                mAllHeight = value;
            }
        }

        private double mAllHeight = 0;

        public void ReSize()
        {
            switch (state)
            {
                case ListState.Idle:
                    double cuY = offset;
                    AllHeight = 0;
                    for (int i = 0; i < Children.Count; i++)
                    {
                        ToDoItem item=Children[i] as ToDoItem;
                        Canvas.SetLeft(item, 0);
                        Canvas.SetTop(item, cuY + item.getDyOffset());
                        double mH = item.MeasureHeight();
                        cuY += mH;
                        AllHeight += mH;
                    }

                    break;
            }
        }
        

        #endregion

        #region scroll

        public double BottomStopOffset()
        {
            if (AllHeight <= ConstVar.ScreenH)
            {

                return 0;
            }

            return ConstVar.ScreenH - AllHeight;
        }

        public double TopStopOffset()
        {
            return 0;
        }

        public double MaxScrollOffset()
        {
            return ConstVar.ListMaxOffest;
        }

        public double MinScrollOffset()
        {
            if (AllHeight <= ConstVar.ScreenH)
            {
                return (-1) * ConstVar.ListMaxOffest;
            }
            else
            {
                return (-1) * (ConstVar.ListMaxOffest + AllHeight - ConstVar.ScreenH);
            }
        }

        public void SlowSpeed()
        {
            //往下滑
            if (speed > 0)
            {
               speed = speed - ConstVar.ListScrollSpeedA;
               if (speed < 0)
                   speed = 0;
            }
            else if(speed<0)
            {
               // 往上滑
                speed = speed + ConstVar.ListScrollSpeedA;
                if (speed > 0)
                    speed = 0;
            }
        }
        #endregion


        #region Drag

        public void RefreshVerDragOffset(Point dPt)
        {
            double dy = (dPt.Y - ToDoCanvas.getInstance().context.DragStartPoint.Y + ToDoCanvas.getInstance().context.DragStartOffset);

            if (dy > MaxScrollOffset())
                dy = MaxScrollOffset();
            else if (dy < MinScrollOffset())
                dy = MinScrollOffset();
            Offset = dy;
            ReSize();
        }

        #endregion

    }
}
